package com.ccys.cms.service.masterData.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ccys.cms.service.masterData.SpecialistCompanyService;
import com.ccys.common.util.Kit;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.masterData.SpecialistCompanyDelDto;
import com.ccys.db.dto.masterData.SpecialistCompanyPageQueryDto;
import com.ccys.db.dto.masterData.SpecialistCompanySaveDto;
import com.ccys.db.entity.TblSpecialistCompany;
import com.ccys.db.mapper.TblSpecialistCompanyMapper;
import com.ccys.db.page.PageDto;
import com.ccys.db.rely.service.impl.BaseServiceImpl;
import com.ccys.db.service.BaseTblAuditTrailWebService;
import com.ccys.db.vo.masterData.SpecialistCompanyListVo;
import com.ccys.db.vo.masterData.SpecialistCompanyPageQueryVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * @program: WhiteCoat
 * @description: 面板管理实现类
 * @author: ZhangJingCan
 * @create: 2023-12-12 14:05
 **/

@Service
public class SpecialistCompanyServiceImpl extends BaseServiceImpl<TblSpecialistCompany> implements SpecialistCompanyService {

    @Resource
    private TblSpecialistCompanyMapper tblSpecialistCompanyMapper;

    /**
     * 用于保存修改状态日志
     */
    @Autowired(required = false)
    private BaseTblAuditTrailWebService baseTblAuditTrailWebService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult save(SpecialistCompanySaveDto dto) {
        Kit.allParamValid(dto,"groupId","name","panelType","panelInstructions");
        //判断 - 一个集团只能存在一个相同名称的数据
        if (ObjectUtil.isNotEmpty(dto.getName()) && ObjectUtil.isNotEmpty(dto.getGroupId())) {
            Example example = new Example(TblSpecialistCompany.class);
            Example.Criteria criteria = example.createCriteria().andEqualTo("groupId", dto.getGroupId())
                    .andEqualTo("name", dto.getName()).andEqualTo("delFlag",0);
            //判断是否有ID - 有ID需要排除自身
            if (ObjectUtil.isNotEmpty(dto.getId())) {
                criteria.andNotEqualTo("id", dto.getId());
            }
            //开始查询并判断 - 如果存在直接返回错误
            if (ObjectUtil.isNotEmpty(tblSpecialistCompanyMapper.selectOneByExample(example))) {
                return ResponseResult.error("The name already exists in the current group!");
            }
        }

        if (ObjectUtil.isNotEmpty(dto.getId()) && ObjectUtil.isNotEmpty(dto.getStatus())) {
            baseTblAuditTrailWebService.saveStatusLog(dto, dto.getStatus());
        }

        //以上校验没问题，开始正常新增或修改 - 并直接返回成功或失败
        return ResponseResult.update(this.insertOrUpdate(dto));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult delete(SpecialistCompanyDelDto dto) {
        //校验是否传入ID - 未传入返回错误信息
        if (ObjectUtil.isEmpty(dto.getId())) {
            return ResponseResult.error("Parameter Error!");
        }
        //准备数据开始软删除
        TblSpecialistCompany company = new TblSpecialistCompany();
        company.setId(dto.getId());
        company.setDelFlag(1);
        if (tblSpecialistCompanyMapper.updateByPrimaryKeySelective(company) > 0) {
            return ResponseResult.success("Delete Successful!");
        }
        return ResponseResult.error("Delete Failed!");
    }

    @Override
    public ResponseResult findPage(SpecialistCompanyPageQueryDto dto) {
        //校验值是否在范围 - 不在则赋值为null
        dto.setStatus(Optional.ofNullable(dto.getStatus()).filter(s -> s > -1 && s < 2).orElse(null));
        dto.setPanelType(Optional.ofNullable(dto.getPanelType()).filter(p -> p > -1 && p < 2).orElse(null));
        //开启分页
        PageDto.startPage();
        PageInfo<SpecialistCompanyPageQueryVo> info = new PageInfo<>(tblSpecialistCompanyMapper.findPage(dto));
        return ResponseResult.success(info);
    }

    @Override
    public ResponseResult<List<SpecialistCompanyListVo>, Object> findList(TblSpecialistCompany dto) {
        List<SpecialistCompanyListVo> list = tblSpecialistCompanyMapper.findList(dto);
        return ResponseResult.success(list);
    }
}
